#include"Queue.h"
void QInit(Queue* pq) {
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
bool QIsEmpty(Queue* pq) {
	assert(pq);
	return pq->phead == NULL;
}

void QPush(Queue* pq, QDataType data) {
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		perror("malloc fail!!");
		exit(1);
	}
	newnode->data = data;
	newnode->next = NULL;
	if (QIsEmpty(pq)) {
		pq->ptail = pq->phead = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
	pq->size++;
}
void QPop(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	if (pq->phead == pq->ptail) {
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else {
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}
void QSize(Queue* pq) {
	assert(pq);
	return pq->size;
}
void QDestroy(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	QNode* pcur = pq->phead;
	while (pcur) {
		QNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
QDataType QFront(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	return pq->phead->data;
}
QDataType QBack(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	return pq->ptail->data;
}